<?php

namespace App\Services;

use App\Exceptions\InternalException;
use App\Models\Quality;
use App\Models\QualityInspect;
use App\Models\Safe;
use App\Models\SafeInspect;
use App\Models\Schedule;
use App\Models\ScheduleRecord;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;

class UserService
{
    public function get($search,$type = 'company'){
        $sql = User::with('role')->with('projects')->withCount('projects')->with('disclosures')->withCount('disclosures')->when($search, function ($query, $search) {
            return $query->where(function($query) use ($search) {
                $query->where('name','like', '%'.$search.'%');
            });
        });

        if($type == 'company'){
            $sql->where('company_id',Auth::user()->company_id);
        }
        return $sql;
    }

    public function userStatisticsList($project_id){
        $sql = User::with('role')->withCount('safes')->withCount('qualitys')->withCount('Schedules')->withCount('disclosures')->withCount('temps')->get();
        return $sql;
    }
    
    public function userUpdate($arr){
        try {
            $id = Auth::user()->id;
            $arr['password'] = Hash::make($arr['password']);
            $data = User::where('id',$id)->update($arr);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function insert($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v || $v == 0){
                $res[$k] = $v;
            }
        }
        try {
            $res['password'] = Hash::make($res['password']);
            $data = User::create($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function update($input){
        $res = [];
        foreach ($input as $k => $v) {
            if($v || $v == 0){
                $res[$k] = $v;
            }
        }
        unset($res['id']);
        try {
            $res['password'] = Hash::make($res['password']);
            $data = User::where('id',$input['id'])->update($res);
            if($data){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function status($data){
        try {
            $data->status =  $data->status ? 0 : 1;
            $data->save();
            if($data->id){
                return $data;
            }
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function userBindProject($input){
        $user = User::find($input['user_id']);
        $projects = json_decode($input['projects']);
        try {
            $user->projects()->sync($projects);
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function userBindDisclosure($input){
        $user = User::find($input['user_id']);
        $disclosures = json_decode($input['disclosures']);
        try {
            $user->disclosures()->sync($disclosures);
        } catch (\Throwable $th) {
            Log::info(json_encode($th));
            throw new InternalException('提交失败');
        }
    }

    public function statisticsThree($project_id){
        $date = Date('Y-m-d H:i:s',time());
        $dateThree = Date('Y-m-d H:i:s',time()+3*24*3600);
        $user_id = Auth::user()->id;
        $Three = [];
        $Three['schedule'] = Schedule::whereDate('start_date','<=',$date)->whereDate('end_date','<=',$dateThree)->where('project_id',$project_id)->where('person_id',$user_id)->count();
        $Three['schedule_complete'] = ScheduleRecord::where('type',3)->where('project_id',$project_id)->where('person_id',$user_id)->count();

        $Three['safe'] = 0;
        $Three['safe_complete'] = SafeInspect::where('project_id',$project_id)->where('person_id',$user_id)->count();

        $Three['quality'] = Quality::whereDate('start_date','<=',$date)->whereDate('end_date','<=',$date)->where('project_id',$project_id)->where('person_id',$user_id)->count();
        $Three['quality_complete'] = QualityInspect::where('project_id',$project_id)->where('person_id',$user_id)->count();
        return $Three;
    }

    public function statisticsOverdue($project_id){
        $date = Date('Y-m-d H:i:s',time());
        $dateThree = Date('Y-m-d H:i:s',time()+3*24*3600);
        $user_id = Auth::user()->id;
        $Overdue = [];
        $Overdue['schedule'] = Schedule::whereDate('end_date','>=',$dateThree)->where('project_id',$project_id)->where('person_id',$user_id)->count();
        $Overdue['schedule_complete'] = ScheduleRecord::where('type',3)->where('project_id',$project_id)->where('person_id',$user_id)->count();

        $Overdue['safe'] = 0;
        $Overdue['safe_complete'] = SafeInspect::where('project_id',$project_id)->where('person_id',$user_id)->count();

        $Overdue['quality'] = Quality::whereDate('start_date','<=',$date)->whereDate('end_date','>=',$date)->where('project_id',$project_id)->where('person_id',$user_id)->count();
        $Overdue['quality_complete'] = QualityInspect::where('project_id',$project_id)->where('person_id',$user_id)->count();
        return $Overdue;
    }

    public function statisticsMaintenance($project_id){
        $user_id = Auth::user()->id;
        $Maintenance = [];
        $Maintenance['safe'] = Safe::where('project_id',$project_id)->where('person_id',$user_id)->count();
        $Maintenance['safe_complete'] = SafeInspect::where('project_id',$project_id)->where('person_id',$user_id)->count();

        $Maintenance['quality'] = Quality::where('project_id',$project_id)->where('person_id',$user_id)->count();
        $Maintenance['quality_complete'] = QualityInspect::where('project_id',$project_id)->where('person_id',$user_id)->count();
        return $Maintenance;
    }

    public function statisticsExamine($project_id){
        $date = Date('Y-m-d H:i:s',time());
        $dateThree = Date('Y-m-d H:i:s',time()+3*24*3600);
        $user_id = Auth::user()->id;
        $Examine = [];
        $Examine['schedule'] = Schedule::whereDate('start_date','<=',$date)->whereDate('end_date','<=',$dateThree)->where('project_id',$project_id)->where('person_id',$user_id)->count();
        $Examine['schedule_complete'] = ScheduleRecord::where('type',3)->where('project_id',$project_id)->where('person_id',$user_id)->count();

        $Examine['safe'] = 0;
        $Examine['safe_complete'] = SafeInspect::where('project_id',$project_id)->where('person_id',$user_id)->count();

        $Examine['quality'] = Quality::whereDate('start_date','<=',$date)->whereDate('end_date','<=',$date)->where('project_id',$project_id)->where('person_id',$user_id)->count();
        $Examine['quality_complete'] = QualityInspect::where('project_id',$project_id)->where('person_id',$user_id)->count();
        return $Examine;
    }
    
}